<?php
// +----------------------------------------------------------------------
// | Description: 系统日志
// +----------------------------------------------------------------------
// | Author: orchief
// +----------------------------------------------------------------------
// | Date: 2018-03-06 13:37:11
// +----------------------------------------------------------------------

namespace App\Model;

use App\Common\Model;

class SysLog extends Model
{
    protected $name = 'admin_syslog';
    protected $insert = [
        'status' => 1,
    ];
    protected $page; // 当前页码
    protected $limit = -1; // 每页数据 -1 标示默认取出所有数据
    protected $fuzzyConditions = ['data', 'URI']; // 模糊查询字段
    protected $clearConditions = ['status', 'uid']; // 精确查询字段
    protected $rangeConditions = ['create_time']; // 范围查询字段

    // 联合查询(对应请求字段)
    protected $union_fuzzyConditions = [
        ['admin_user', 'username'],
    ];
    // 联合查询 基础连接
    protected $foreignConditions = [
        'admin_user' => ['id', 'uid'],
    ];
    // 返回字段
    protected $returnFields = 'admin_user.username,admin_syslog.*';

    /**
     * 过滤数据
     */
    public function filter($data)
    {
        $list = $data['list'];
        foreach ($list as $k => $v) {
            $list[$k]['create_time'] = date("Y-m-d H:i:s", $v['create_time']);
        }
        $data['list'] = null;
        $data['list'] = $list;
        return $data;
    }

    /* 需要实现多条件查询的模块 将这里复制即可 */

    /**
     * 查询某个用户的日志列表
     * @param $uid  用户id
     * @param $page 页码
     * @param $limit 每页条目
     */
    public function getDataByUId($uid, $page, $limit)
    {
        $map = ['uid' => $uid];
        $list = $this->where($map);

        $dataCount = $list->count();
        // 若有分页
        if ($page && $limit) {
            $list = $list->page($page, $limit);
        }

        $listData = $this
            ->alias('log')
            ->where($map)
            ->join('__ADMIN_USER__ user', 'log.uid=user.id', 'LEFT')
            ->field('log.*, user.username as username')
            ->select();

        $data['list'] = $listData;
        $data['dataCount'] = $dataCount;

        return $data;
    }

    /**
     * [delDataById 根据id删除数据]
     * @linchuangbin
     * @DateTime  2017-02-11T20:57:55+0800
     * @param     string                   $id     [主键]
     * @param     boolean                  $delSon [是否删除子孙数据]
     * @return    [type]                           [description]
     */
    public function delDataById($id = '', $delSon = false)
    {
        try {
            $this->where($this->getPk(), '=', $id)->delete();
            return true;
        } catch (\Exception $e) {
            $this->error = '删除失败';
            return false;
        }
    }
}
